define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    

    $.widget('rk.scheduleCurtainCtrl', $.rk.oaCurtainCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._initScheduleCurtain();
            elem.on('click', 'a[act="closeScheduleCurtain"]', function () {
                me.closeMe();
            });
            elem.on('click', 'a[act]', function (evt) {
                evt.preventDefault();
                var $lnk = $(this),
                    act = $lnk.attr('act'),
                    sid = $lnk.attr('scheduleid');
                if (act == 'accept') {
                    $.post('/json/crm_schedule/accept.action', {
                        scheduleId: sid
                    }, null, 'json').done(function (json) {
                        if (json.status == '0') {
                            $.msg(rk.i18n('SCHEDULE_32'),1);
                            $lnk.closest('div.schedule_confirm_detail').slideUp('fast', function () {
                                $(this).remove();
                                if (!$('#div_schedule_unchecked_list').find('div.schedule_confirm_detail').size()) {
                                    me._delay(function () {
                                        elem.find('a[act="closeScheduleCurtain"]').trigger('click');
                                    });

                                    //隐藏未确认条数
                                    $("a[act=scheduleCurtain]").eq(0).hide();
                                } else {
                                    //设置条数
                                    $("span[act=uncheck_number]:eq(0)").text($('#div_schedule_unchecked_list').find('div.schedule_confirm_detail').size());
                                }
                            });
                        } else {
                            $.msg(rk.i18n('OPERATE_LOSE')+'：' + json.statusText);
                        }
                    });
                } else if (act == 'reject') {
                    rk.dialog({
                        title: rk.i18n('SCRPT_TMPL_SCHEDULE_REJECTREASON_1'),
                        width: 480,
                        success: function (dialogElem, footer) {
                            var html = rk.templateText(require('oa/tmpl/schedule/tpl_schedule_dialog_reject_reason.tpl'), {});
                            dialogElem.html(html);
                            setTimeout(function () {
                                dialogElem.find("textarea.js-cause").focus();
                            }, 100);
                            footer.find('a[act=saveReject]').on('click', function (evt) {
                                evt.preventDefault();
                                var reason = dialogElem.find("textarea.js-cause").val();
                                if (!reason) {
                                    return $.msg(rk.i18n('SCRPT_TMPL_SCHEDULE_REJECTREASON_3'));
                                }
                                $.post('/json/crm_schedule/reject.action', {
                                    scheduleId: sid,
                                    reason: reason
                                }, null, 'json').done(function(json) {
                                    if (json.status == '0') {
                                        $.msg(rk.i18n('SCHEDULE_33'), 1);
                                        $lnk.closest('div.schedule_confirm_detail').slideUp('fast', function() {
                                            $(this).remove();
                                            if (!$('#div_schedule_unchecked_list').find('div.schedule_confirm_detail').size()) {
                                                me._delay(function () {
                                                    elem.find('a[act="closeScheduleCurtain"]').trigger('click');
                                                }, 500);

                                                //隐藏未确认条数
                                                $("a[act=scheduleCurtain]").eq(0).hide();
                                            } else {
                                                //设置条数
                                                $("span[act=uncheck_number]:eq(0)").text($('#div_schedule_unchecked_list').find('div.schedule_confirm_detail').size());
                                            }
                                        });
                                        rk.closeDialog();
                                    } else {
                                        $.msg(rk.i18n('OPERATE_LOSE')+'：' + json.statusText);
                                    }
                                });
                            });
                            footer.find('a[act=closeReject]').on('click', function () {
                                rk.closeDialog();
                            });
                        },
                        footer: [{
                            act: 'closeReject', visible: true, text: rk.i18n('CANCEL'), color: 'white'
                        }, {
                            act: 'saveReject', visible: true, text: rk.i18n('SCRPT_TMPL_SCHEDULE_REJECTREASON_4')
                        }]
                    });
                }
            });
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },
        _initScheduleCurtain: function () {
            var me = this;
            var elem = me.element;
            rk.post('/json/crm_schedule/unchecked-list.action', {}).done(function (json) {
                if (json.status == 0) {
                    var data = json.data;
                    if (!data.schedules.length) {
                        //$('#div_schedule_unchecked_list').after('<div class="content_error"><p>您当前没有未确认的日程</p></div>').hide();
                        return;
                    }
                    $.each(data.schedules, function(i, o) {
                        o.creater = data.users[o.createdBy].name;
                        var cd = new Date(+o.createdAt),
                            mcd = moment(cd);
                        o.createdDate = mcd.format('YYYY-MM-DD');
                        o.createdTime = mcd.format('HH:mm');
                        var sd = new Date(+o.startDate),
                            msd = moment(sd);
                        var ed = new Date(+o.endDate),
                            med = moment(ed);
                        o.duration = $.map((function() {
                            var sd = msd.format('YYYY-MM-DD'),
                                st = msd.format('HH:mm'),
                                ed = med.format('YYYY-MM-DD'),
                                et = med.format('HH:mm');
                            if (o.isAllDay == '1') {
                                if (sd == ed) {
                                    return [sd];
                                } else {
                                    return [sd, rk.i18n('TO'), ed];
                                }
                            } else {
                                if (sd == ed) {
                                    if (st == et) {
                                        return [sd, st];
                                    } else {
                                        return [sd, st, rk.i18n('TO'), et];
                                    }
                                } else {
                                    return [sd, st, rk.i18n('TO'), ed, et];
                                }
                            }
                        })(), function(s) {
                            return '<span>' + s + '</span>';
                        }).join('');
                        var oUser = {};
                        o.acceptMember = $.map(o.acceptMember || [], function(uid) {
                            oUser = data.users[uid] || {};
                            return '<li><a href="/final/user.action?uid=' + uid + '" title="' + oUser.name + '"><img src="' + (oUser.icon || DEFAULTS.USER_DEFAULT_ICON) + '"/></a></li>';
                            //return (oUser.icon || DEFAULTS.USER_DEFAULT_ICON);
                        }).join('');
                        if (o.acceptMember) {
                            o.members = '<ul class="task_people_list">' + o.acceptMember + '</ul>';
                        }
                        o.waitingMember = $.map(o.waitingMember || [], function(uid) {
                            oUser = data.users[uid] || {};
                            return '<li><a href="/final/user.action?uid=' + uid + '" title="' + oUser.name + '"><img src="' + (oUser.icon || DEFAULTS.USER_DEFAULT_ICON) + '"/></a></li>';
                            //return (oUser.icon || DEFAULTS.USER_DEFAULT_ICON);
                        }).join('');
                        if (o.waitingMember) {
                            o.waiting = '<div class="confirmed_people participants"><span class="tip">'+rk.i18n('SCHEDULE_28')+'</span><ul class="task_people_list">' + o.waitingMember + '</ul></div>';
                        }
                        o.rejectMember = $.map(o.rejectMember || [], function(uid) {
                            oUser = data.users[uid] || {};
                            return '<li><a href="/final/user.action?uid=' + uid + '" title="' + oUser.name + '"><img src="' + (oUser.icon || DEFAULTS.USER_DEFAULT_ICON) + '"/></a></li>';
                            //return (oUser.icon || DEFAULTS.USER_DEFAULT_ICON);
                        }).join('');
                        if (o.rejectMember) {
                            o.reject = '<div class="confirmed_people participants"><span class="tip">'+rk.i18n('SCHEDULE_29')+'</span><ul class="task_people_list">' + o.rejectMember + '</ul></div>';
                        }
                    });
                    var listHtml = rk.templateText(require('oa/tmpl/schedule/tpl_schedule_curtain_unchecked.tpl'), json.data);
                    elem.html(listHtml);
                    elem.find('.oa-curtain-body').beScroll();
                } else {
                    $.msg(rk.i18n('REQUEST_ERROR'));
                }
            });
        }
    });

    return $;
});